#include "SystemSDL_1_2.h"

#include <SDL.h>

static const unsigned int ARRAY_BOUNDS = 64;

static const char HW_AVAIL[2][ARRAY_BOUNDS] =
{
    "Hardware surfaces are not available",
    "Hardware surfaces are available"
};

static const char WM_AVAIL[2][ARRAY_BOUNDS] =
{
    "Window manager is not available",
    "Window manager is available"
};

static const char HW_ACCEL[2][ARRAY_BOUNDS] =
{
    "Hardware accelerated blits not available",
    "Hardware accelerated blits is available"
};

static const char HW_ACCEL_CC[2][ARRAY_BOUNDS] =
{
    "Hardware accelerated blits with color key not available",
    "Hardware accelerated blits with color key available"
};

static const char HW_ACCEL_A[2][ARRAY_BOUNDS] =
{
    "Hardware accelerated blits with alpha are not available",
    "Hardware accelerated blits with alpha are available"
};

static const char SW_BLIT[2][ARRAY_BOUNDS] =
{
    "Software blits are not accelerated",
    "Software blits are accelerated"
};

static const char SW_BLIT_CC[2][ARRAY_BOUNDS] =
{
    "Software accelerated blits with color key are not available",
    "Software accelerated blits with color key are available"
};

static const char SW_BLIT_A[2][ARRAY_BOUNDS] =
{
    "Software accelerated blits with alpha are not available",
    "Software accelerated blits with alpha are available"
};

static const char BLIT_FILL[2][ARRAY_BOUNDS] =
{
    "Color fills are not accelerated",
    "Color fills are accelerated"
};

bool            CSystemSDL_1_2_Video::m_isRunning = false;
SDL_Surface*    CSystemSDL_1_2_Video::m_pFocusWnd = NULL;
const SDL_VideoInfo*  CSystemSDL_1_2_Video::m_pVideoInfo = NULL;

int CSystemSDL_1_2_Video::Init( void )
{
    if( SDL_InitSubSystem( SDL_INIT_VIDEO ) < 0 )
    {
        // an error as occurred, handle it with care
        return -1;
    }

    m_pVideoInfo = SDL_GetVideoInfo();

#ifdef PRINT_VIDINFO
    PrintVidInfo();
#endif

    if( m_pVideoInfo == NULL )
    {
        //  an error occurred, handle it
        return -1;
    }
    // all is well
    m_isRunning = true;

    return 0;
}

int CSystemSDL_1_2_Video::Process( void )
{
    if( SDL_PollEvent( NULL )!= 0 )
    {
        SDL_Event ev;

        if( SDL_WaitEvent( &ev ) == 0 )
        {
            // an error occurred, handle it with care
            return -1;
        }

#ifdef  PRINT_EVENT
        CSystemSDL_1_2_Helper::PrintEvent( ev );
#endif

        if( ev.type == SDL_QUIT )
            m_isRunning = false;
    }

    // check the message queue, without pumping the message loop to see if there are any left
    return SDL_PeepEvents( NULL, 1, SDL_PEEKEVENT, SDL_ALLEVENTS );
}

void CSystemSDL_1_2_Video::ShutDown( void )
{
    m_isRunning = false;
    m_pFocusWnd = NULL;

    if( SDL_WasInit( SDL_INIT_VIDEO ) != 0 )
        SDL_QuitSubSystem( SDL_INIT_VIDEO );
}

void CSystemSDL_1_2_Video::PrintVidInfo( void )
{
    CSystemSDL_1_2_Helper::Printf( "Video Info:\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n\t%s\n"
                                   "\t%s\n\t%s\n\t%s\n\tAvailable video mem %dK\n"
                                   "\tDesktop %dx%d\n",
                                   HW_AVAIL[m_pVideoInfo->hw_available],
                                   WM_AVAIL[m_pVideoInfo->wm_available],
                                   HW_ACCEL[m_pVideoInfo->blit_hw],
                                   HW_ACCEL_CC[m_pVideoInfo->blit_hw_CC],
                                   HW_ACCEL_A[m_pVideoInfo->blit_hw_A],
                                   SW_BLIT[m_pVideoInfo->blit_sw],
                                   SW_BLIT_CC[m_pVideoInfo->blit_sw_CC],
                                   SW_BLIT_A[m_pVideoInfo->blit_sw_A],
                                   BLIT_FILL[m_pVideoInfo->blit_fill],
                                   m_pVideoInfo->video_mem, m_pVideoInfo->current_w,
                                   m_pVideoInfo->current_h );
}
